/*
 * Adrian Martinez 11248
 * Maria Fernanda Martinez 11176
 * Rodrigo Mejia 11111
 */
package hoja7;

/* 
  Código Basado en: 
 
 * Copyright (c) 2012 the authors listed at the following URL, and/or
 the authors of referenced articles or incorporated external code:
 http://en.literateprograms.org/Hash_table_(Java)?action=history&offset=20080129055006
 
 Permission is hereby granted, free of charge, to any person obtaining
 a copy of this software and associated documentation files (the
 "Software"), to deal in the Software without restriction, including
 without limitation the rights to use, copy, modify, merge, publish,
 distribute, sublicense, and/or sell copies of the Software, and to
 permit persons to whom the Software is furnished to do so, subject to
 the following conditions:
 
 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Retrieved from: http://en.literateprograms.org/Hash_table_(Java)?oldid=12352
 */

public class HashTable implements WordSet {
    
    private Hashnode[] nodes;

    @SuppressWarnings("unchecked")
    public HashTable(int size)
    {
        nodes = new Hashnode[size];
    }
    
    
    /* We will add a method for obtaining the index into our table for a particular 
     * hash key. To make sure the hash value is not bigger than the size of the 
     * table, the result of the user provided hash function is used modulo the 
     * length of the table. We need the index to be non-negative, but the modulus 
     * operator (%) will return a negative number if the left operand (the hash 
     * value) is negative, so we have to test for it and make it non-negative. */
    
    private int getIndex(Word key)
    {
        int hash = key.hashCode() % nodes.length;
        if (hash < 0)
            hash += nodes.length;
        return hash;
    }
    
    public Word insert(Word key)
    {
        int hash = getIndex(key);
        
        for (Hashnode node = nodes[hash]; node != null; node = node.next) {
            if (key.equals(node.data)) {
                Word oldData = node.data;
                node.data = key;
                return oldData;
            }
        }
        
        Hashnode node = new Hashnode(key, nodes[hash]);
        nodes[hash] = node;
        
        return null;
    }
    
    public boolean remove(Word key)
    {
        int hash = getIndex(key);

        Hashnode prevnode = null;
        for (Hashnode node = nodes[hash]; node != null; node = node.next) {
            if (key.equals(node.data)) {
                if (prevnode != null)
                    prevnode.next = node.next;
                else
                    nodes[hash] = node.next;
                return true;
            }
            prevnode = node;
        }
        
        return false;
    }
    
    /* Searching for an element is easy. We just search through the linked list for 
     * the corresponding hash value. null is returned if we didn't find it.
     */
    public Word get(Word key)
    {
        int hash = getIndex(key);
        
        for (Hashnode node = nodes[hash]; node != null; node = node.next) {
            if (key.equals(node.data))
                return node.data;
        }
        
        return null;
    }
    
    public void add(Word wordObject) {
        insert(wordObject);
    }
    
    
   

    
}
